package org.moonlight.jvm.classfile.attribute;

import lombok.Getter;
import org.moonlight.jvm.classfile.ClassReader;

/**
 * BootstrapMethods属性在Java1.7以后才有，位于类文件的属性列表中，这个属性用于保存 invokedynamic 指令引用的引导方法限定符。
 * 其结构如下:
 *   BootstrapMethods_attribute {
 *     u2 attribute_name_index;
 *     u4 attribute_length;
 *     u2 num_bootstrap_methods;
 *     {
 *         u2 bootstrap_method_ref;
 *         u2 num_bootstrap_arguments;
 *         u2 bootstrap_arguments[num_bootstrap_arguments];
 *     } bootstrap_methods[num_bootstrap_methods];
 * }
 *
 * @author Moonlight
 * @createTime 2023/8/15 17:32
 **/
@Getter
public class BootstrapMethodsAttribute implements AttributeInfo {

    private BootstrapMethod[] bootstrapMethods;


    @Override
    public void readInfo(ClassReader reader) {
        int numBootstrapMethods = reader.readUint16();
        BootstrapMethod[] bootstrapMethods = new BootstrapMethod[numBootstrapMethods];
        for (int i = 0; i < numBootstrapMethods; i++) {
            bootstrapMethods[i] = new BootstrapMethod( reader.readUint16(), reader.readUint16s() );
        }
        this.bootstrapMethods = bootstrapMethods;
    }

    @Getter
    static class BootstrapMethod {
        private final int bootstrapMethodRef;
        private final int[] bootstrapArguments;

        BootstrapMethod(int bootstrapMethodRef, int[] bootstrapArguments) {
            this.bootstrapMethodRef = bootstrapMethodRef;
            this.bootstrapArguments = bootstrapArguments;
        }
    }

}
